//
//  SettingController.m
//  庐山通
//
//  Created by mac22 on 14/10/19.
//  Copyright (c) 2014年 郑朝军. All rights reserved.
//

#import "SettingController.h"
#import "MyWaterFlow.h"
#import "FavatorCell.h"
#import "MJRefresh.h"
#import "SelectFindHttp.h"
#import "HotModel.h"
#import "HotModelFrame.h"
#import "MJExtension.h"
#import "HotDecatilViewController.h"
#import "Grouble.h"
#import "MyNavigation.h"


#define myflow @"flowscou"
@interface SettingController()<UICollectionViewDelegate,UICollectionViewDataSource,MyWaterFlowDelegate,MJRefreshBaseViewDelegate>
@property (nonatomic,weak)UICollectionView * flowCollectionView;

@property (nonatomic,strong) MJRefreshHeaderView *header;
@property (nonatomic,strong) MJRefreshFooterView *footer;


@property (nonatomic,strong)NSMutableArray *dataFrame;
@property (nonatomic,strong)NSMutableArray *tempArray;
@end
@implementation SettingController

- (NSMutableArray *)tempArray
{
    if (_tempArray == nil) {
        _tempArray = [[NSMutableArray alloc]init];
    }
    return _tempArray;
}
- (NSMutableArray *)dataFrame
{
    if (_dataFrame == nil) {
        _dataFrame = [NSMutableArray arrayWithCapacity:100];
    }
    return _dataFrame;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
   
    [self addCooectionView];
    [self addHeaderRush];
    
}

- (void)addHeaderRush
{
    MJRefreshHeaderView * head = [[MJRefreshHeaderView alloc]init];
    head.scrollView = self.flowCollectionView;
    head.delegate = self;
    [head beginRefreshing];
    self.header = head;
    MJRefreshFooterView * foot = [[MJRefreshFooterView alloc]init];
    foot.scrollView = self.flowCollectionView;
    foot.delegate = self;
    self.footer = foot;
}
// 开始刷新
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) { // 上拉刷新
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}
- (void)loadMoreData
{
    [SelectFindHttp getWirhUrl:courenao parms:nil success:^(id json) {
        
        NSMutableArray *bannerFrame=[NSMutableArray array];
        
        //ZCJLog(@"%@",json);
        
        // 获取模型对象
        NSArray *findArrayTemp = [HotModel objectArrayWithKeyValuesArray:json];
        
        for(HotModel * findModel in findArrayTemp){
            HotModelFrame * findModelFrame = [[HotModelFrame alloc]init];
            
            findModelFrame.hotModel = findModel;
            [bannerFrame addObject:findModelFrame];
        }
        self.dataFrame = bannerFrame;
        [self.flowCollectionView reloadData];
        [self.header endRefreshing];
        [MBProgressHUD showSuccess:@"上拉刷新成功"];
    } failture:^(id error) {
        NSLog(@"%@",error);
    }];

}

- (void)loadNewData
{
    [SelectFindHttp getWirhUrl:courenao parms:nil success:^(id json) {
        
        NSMutableArray *bannerFrame=[NSMutableArray array];
        
        //ZCJLog(@"%@",json);
        
        // 获取模型对象
        NSArray *findArrayTemp = [HotModel objectArrayWithKeyValuesArray:json];
        
        for(HotModel * findModel in findArrayTemp){
            HotModelFrame * findModelFrame = [[HotModelFrame alloc]init];
            
            findModelFrame.hotModel = findModel;
            [bannerFrame addObject:findModelFrame];
        }
        self.dataFrame = bannerFrame;
        [self.flowCollectionView reloadData];
        [self.header endRefreshing];
        [MBProgressHUD showSuccess:@"上拉刷新成功"];
    } failture:^(id error) {
        NSLog(@"%@",error);
    }];

}
- (void)addCooectionView
{
    // collection的一个布局
    MyWaterFlow * flow = [[MyWaterFlow alloc]init];
    // 拿到每一个格子的size
    flow.delegate = self;
    flow.columsCount = 2;
    flow.sectionInset = UIEdgeInsetsMake(0, 10, 40, 10);
    
    UICollectionView * flowCollectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flow];
    
    flowCollectionView.backgroundColor = ZCJColor(237, 237, 237);
    
    // 注册一个cell
    [flowCollectionView registerClass:[FavatorCell class] forCellWithReuseIdentifier:myflow];
    
    flowCollectionView.delegate = self;
    flowCollectionView.dataSource = self;
    [self.view addSubview:flowCollectionView];
    self.flowCollectionView = flowCollectionView;
    
}
// 设置每一个item的宽高
- (CGFloat)waterFlow:(MyWaterFlow *)waterFlow heightForWidth:(CGFloat)width indexPath:(NSIndexPath *)indexPath
{
    HotModelFrame * modelFrame = self.dataFrame[indexPath.row];
    
    return modelFrame.cellH;
}
// 每一个item点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotModelFrame * modelFrame = self.dataFrame[indexPath.row];
    HotDecatilViewController * hotDecatVC = [[HotDecatilViewController alloc]init];
    
    hotDecatVC.hotModel = modelFrame.hotModel;
    
    MyNavigation * nav = [[MyNavigation alloc]initWithRootViewController:hotDecatVC];
    [self presentViewController:nav animated:YES completion:nil];
    

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataFrame.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FavatorCell * cell = [FavatorCell FavatorCell:collectionView cellForItemAtIndexPath:indexPath];
    //self.dataFrame[indexPath.row];
    HotModelFrame * hotModleFrame = self.dataFrame[indexPath.row];
    
    
    cell.hotModel = hotModleFrame;
    
    return cell;
}

- (void)dealloc
{
    [self.footer free];
    [self.header free];
}
@end
